package eco2automotive.elmFilter;
import eco2automotive.util.MathLib;
import java.util.*;

/**  
  * This class is used to interpret a Special Parameter value.</br>
  * Eg. Check the MIL Ligth
  * @version 1.0
  * @autor Fco. Javier Sánchez Esparza
  *
  */ 
public class SpecialFormula {

 /**  
  * Check the MIL Ligth statuts and calculate the number of trouble codes.
     * </br>str= (No header: 4101) 81076504 - 
     * Check MIL STATUS and return the current Diagnostics Trouble Codes.                     
     * </br> Byte number 1: 81(hex) double function
     * </br> First Function     : The most significant bit.. to indicate 
              that the malfunction indicator lamp has been ON, 81 = [1]000001, 1 = ON status
     * </br>Second Function    :
     * </br> 81: is the number of current trouble codes  
     * </br> 81 (hex) = 129(dec) >>> substract 80(hex)= 128; 
     * </br> Operation= 129 - 128 = 1 , 1 Trouble Code found.         
     * 
     * @param str string to evaluate
  * @return num_of_TC , the number of trouble codes founds.
  */     
       
public int pid_0101(String str)            
    {
        
        int num_of_TC;               
        char ON_OFF;    
        String first_byte;
        first_byte=new String(str.substring(0,1));
        MathLib operations= new MathLib();                
        ON_OFF=operations.HexToBin(first_byte).charAt(0);//Bit 0
        if (ON_OFF=='1')//Then the lamp is ON
        {
            double d = Double.valueOf(operations.calculatorXF(str,"AA","A-128"));
            num_of_TC= (int)d;
            if (num_of_TC<0) num_of_TC=0;
        }
        else        
            num_of_TC=0;        
                
        return  num_of_TC;
    }


/**  
 * Returns a array with the errors founds  
* </br>str= (No header: 43) 01 33 00 00 00 00          
* </br>Group by 2 bytes : 0133,0000,0000 
      * @param str string to evaluate
 * @return errors eg. P0107 
 * @see eco2automotive.data/ErrorTroubleCodes.xml
 * 
  */

public ArrayList pid_03(String str)
{  
  
 ArrayList errors=new ArrayList(); 
 int length= str.length();        

 if ((str.length() % 4) == 0) //Grupos de 4 
   for(int i=0; i<(str.length());i=i+4){                                   
       //See documentation... The first hex digit received
       String var=str.substring(i,i+4);
       String head="";
       boolean no_replacement=false;
       switch(var.charAt(0)){
           case '0':{head="P0";break;}case '1':{head="P1";break;}    
           case '2':{head="P2";break;}case '3':{head="P3";break;}
           case '4':{head="C0";break;}case '5':{head="C1";break;}
           case '6':{head="C2";break;}case '7':{head="C3";break;}
           case '8':{head="B0";break;}case '9':{head="B1";break;}
           case 'A':{head="B2";break;}case 'B':{head="B3";break;}               
           case 'C':{head="U0";break;}case 'D':{head="U1";break;}               
           case 'E':{head="U2";break;}case 'F':{head="U3";break;}               
           default: {no_replacement=true;break;}           
       }
       
       if (no_replacement==true)           
        errors.add(var);   
       else
           errors.add(head+var.substring(1));
   }
 else{ //Return not separated
  errors.add(str);
 }
      return errors;
}

/**  
 * Returns a String with information about the standar supported by the vehicle.
     * </br> 0x01  00000001b    OBD-II as defined by the CARB
     * </br>0x02  00000010b    OBD as defined by the EPA
     * </br>0x03  00000011b    OBD and OBD-II
     * </br>0x04  00000100b    OBD-I
     * </br>0x05  00000101b    Not meant to comply with any OBD standard
     * </br>0x06  00000110b    EOBD (Europe)
     * </br>0x07  00000111b    EOBD and OBD-II
     * </br>0x08  00001000b    EOBD and OBD
     * </br>0x09  00001001b    EOBD, OBD and OBD II
     * </br>0x0A  00001010b    JOBD (Japan)
     * </br>0x0B  00001011b    JOBD and OBD II
     * </br>0x0C  00001100b    JOBD and EOBD
     * </br>0x0D  00001101b    JOBD, EOBD, and OBD II     
     * @param str string to evaluate
 * @return standar , Information about the standar
  */
public String pid_011C(String str){

String standar="Unknow";
    switch(str.charAt(1)){
           case '1':{standar="OBD-II as defined by the CARB";break;}    
           case '2':{standar="OBD as defined by the EPA";break;}
           case '3':{standar="OBD and OBD-II";break;}
           case '4':{standar="OBD-I";break;}
           case '5':{standar="Not meant to comply with any OBD standard";break;}
           case '6':{standar="EOBD (Europe)";break;}
           case '7':{standar="EOBD and OBD-II";break;}
           case '8':{standar="EOBD and OBD";break;}
           case '9':{standar="EOBD, OBD and OBD II";break;}
           case 'A':{standar="JOBD (Japan)";break;}
           case 'B':{standar="JOBD and OBD II";break;}               
           case 'C':{standar="JOBD and EOBD";break;}
           case 'D':{standar="JOBD, EOBD, and OBD II";break;}               
           default: {break;}           
       }
    
        return standar;
}

    public static void main( String[] args ) {   
        ArrayList a= new ArrayList();
        
        System.out.println("eg. Number of Trouble Codes Founds "+ new SpecialFormula().pid_0101("81076504"));
        a= new SpecialFormula().pid_03("017300000000");
          
        
            for ( Iterator iterador = a.listIterator(); iterador.hasNext(); ) {
                    System.out.println((String) iterador.next());                                      
                    }  
            System.out.println(new SpecialFormula().pid_011C("06"));
       
}    
    
}
